/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package promogui;


import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Friedemann Metzger
 */
public class HealthCriteria {
    private String name;
    
    private int zaehler, nenner, faktor;
    
    private boolean flag1, flag2;
    
    private int opt;
    
    private double value1, value2;
    

    public HealthCriteria(int zaehler, int nenner, int faktor, int opt, boolean flag1, double value1, boolean flag2, double value2, String name) {
        this.zaehler = zaehler;
        this.nenner = nenner;
        this.faktor = faktor;
        this.opt = opt;
        this.value1 = value1;
        this.value2 = value2;
        this.flag1 = flag1;
        this.flag2 = flag2;
        this.name = name;    
    }
    
    
    
    

    
    
public ArrayList<Double> calc(Project project)
{
    ArrayList<Double> values = new ArrayList<Double>();
    
    
    
   for (int i = 0; i < project.getCurrentWeek(); i++)
    {
        
        double z = getValue(project, zaehler, i);
        double n = getValue(project, nenner, i);
        double f= getValue(project, faktor, i);
        
        if (n == 0)
        {
            values.add(new Double(0)); 
        }
        else
        {
            double y  = (z / n) * f;
            values.add(new Double(y)); 
        }
    }
    
    
    return values;

    
}
    
public boolean isFitNow(Project project)
{

    ArrayList<Double> values = calc(project);
    int week = values.size();
    double value = values.get(week - 1);



    return isFitHelper(value, project, week-1);

}

public ArrayList<Boolean> isFit(Project project)
{
    ArrayList<Boolean> list = new ArrayList<Boolean>();
    
    int weeks = project.getCurrentWeek();
    ArrayList<Double> values = calc(project);
    for (int i = 0; i < weeks; i++) 
    {
        list.add(isFitHelper(values.get(i),project, i));    
    }
    
    
    
    return list;
}


public ArrayList<Boolean> isFitOverview(ArrayList<Project> projects)
{
    ArrayList<Boolean> list = new ArrayList<Boolean>();
    
    Iterator<Project> it = projects.iterator();
    
    while (it.hasNext())
    {
        list.add(isFitNow(it.next()));    
    }
    
    
    
    return list;
}



public ArrayList<Double> calcHealthOverviewValues(ArrayList<Project> projects)
{
    
ArrayList<Double> list = new ArrayList<Double>();    
int projCount = projects.size();
Iterator<Project> it = projects.iterator();

while(it.hasNext())
{
    Project project = it.next();
    int weekCount = project.getCurrentWeek();
    list.add(calc(project).get(weekCount - 1)); //hex hex

}



return list;
}





private double getValue(Project project, int index, int week)
{
//    switch (index)
//    {
//    
//        case 0:
//            return project.getLinesOfCode().get(week);
//        case 1:
//            return project.getNumberOfTasks().get(week);
//        case 2:
//            return project.getNumberOfBugs().get(week);
//        case 3:
//            return project.getNumberOfComponents().get(week);
//        case 4:
//            return project.getNumberOfTestCases().get(week);
//   
//        case 5:
//            return project.getPercentageOfSuccessfulTests().get(week);
//        case 6:
//            return project.getPercentageOfCodeCoverage().get(week);
//            
//        default: return 1;
//    
//    
//    }
    
    
    return AttributeChooser.getValue(project, index, week);

}





public boolean isFitHelper(double value, Project project, int week){
         
        double a, b;
        if(flag1)        
            a = value1;
        else
            a = getValue(project, (int) value1, week);
        
        
        
        if(flag2)        
            b = value2;
        else
            b = getValue(project, (int) value2, week);
    
  switch (opt)
  {

        case 0:
                       
            if(value <= a)
                return true;
            else
                return false;
    
            
            
        case 1:
            if(value >= b)
                return true;
            else
                return false;
        case 2:
            
            if(value <= a && value >= b )
                return true;
            else
                return false;


                 
        default: return false;
    
  }


    
  
    
}

public String getName()
{
    return name;
}

}
